import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_train.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';
import 'package:get/get.dart';

import '../common/common_dialog.dart';
import 'controller/eacher_work_train_details_controller.dart';
import 'model/train_model.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/14.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class TrainTeacherCourseItemWidget4 extends StatefulWidget {
  final CourseAndTeacherBean cubean;
  final int index;

  final isScaffold;

  const TrainTeacherCourseItemWidget4(
      {Key key, this.cubean, this.index, this.isScaffold = true})
      : super(key: key);

  @override
  _TrainTeacherCourseItemWidget4State createState() =>
      _TrainTeacherCourseItemWidget4State();
}

class _TrainTeacherCourseItemWidget4State
    extends State<TrainTeacherCourseItemWidget4>
    with SingleTickerProviderStateMixin, TrainModel {
  @override
  void initState() {
    super.initState();
  }

  List<CourseBean> _list = [];

  @override
  Widget build(BuildContext context) {
    String userName = "无教师";
    if (widget.cubean.userEntity != null) {
      userName = widget.cubean.userEntity.reamName;
    }
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(top: 12, right: 12, bottom: 20),
      margin: EdgeInsets.only(top: 6),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            padding: EdgeInsets.only(right: 6, left: 6),
            margin: EdgeInsets.only(top: 3),
            child: Text("${widget.cubean.index}"),
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(
                  children: [
                    Expanded(
                        child: Text(
                      "教师 $userName",
                      style: TextStyle(fontWeight: FontWeight.bold),
                    )),
                    Text("${widget.cubean.teacherPeriod}课时"),
                  ],
                ),
                Container(
                  color: Colors.grey[200],
                  width: double.infinity,
                  margin: EdgeInsets.only(top: 4),
                  padding: EdgeInsets.all(4),
                  child: Wrap(
                    crossAxisAlignment: WrapCrossAlignment.start,
                    children: [
                      Text(
                        "培训内容:",
                      ),
                      SizedBox(
                        width: 12,
                      ),
                      Text(
                        widget.cubean.teacherContent,
                        style: TextStyle(
                          fontSize: 14,
                          color: Color(0xff666666),
                        ),
                      ),
                    ],
                  ),
                ),
                buildStartTimeWidget(),
                buildCurrentSignNumberWidget(),
                buildTeacherSginWidget(),
                buildBottomButtonRow(),
              ],
            ),
          ),
        ],
      ),
    );
  }

  buildStartTimeWidget() {
    if (widget.cubean.teacherContent == null) {
      return SizedBox();
    }
    return Container(
      color: Colors.grey[200],
      padding: EdgeInsets.all(8),
      margin: EdgeInsets.only(top: 8),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            "培训时间:",
          ),
          Row(
            children: [
              Text(
                "${widget.cubean.beginTime} ~ ",
                style: TextStyle(fontSize: 12, color: Color(0xff666666)),
              ),
              Text(
                "${widget.cubean.endTime} ",
                style: TextStyle(fontSize: 12, color: Color(0xff666666)),
              ),
            ],
          ),
        ],
      ),
    );
  }

  ///当前签到人数
  buildCurrentSignNumberWidget() {
    if (widget.cubean.signUserNumber == null) {
      return SizedBox();
    }
    return Container(
      color: Colors.grey[200],
      padding: EdgeInsets.all(8),
      margin: EdgeInsets.only(top: 8),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            "当前签到人数:",
          ),
          SizedBox(width: 8,),
          Text(
            "${widget.cubean.signUserNumber} 人 ",
            style: TextStyle(fontSize: 14, color: Color(0xff666666)),
          ),
        ],
      ),
    );
  }

  buildTeacherSginUpTimeWidget() {
    if (!widget.cubean.showTeacherTime ||
        widget.cubean.teacherSignEndTime == null) {
      return SizedBox();
    }
    return Container(
      margin: EdgeInsets.only(top: 4),
      child: Wrap(
        crossAxisAlignment: WrapCrossAlignment.center,
        children: [
          Text(
            "结束上课时间： ",
            style: TextStyle(fontSize: 14, color: Color(0xff666666)),
          ),
          Text(
            "${widget.cubean.teacherSignEndTime} ",
            style: TextStyle(fontSize: 14, color: Color(0xff666666)),
          ),
        ],
      ),
    );
  }

  buildTeacherSginWidget() {
    if (!widget.cubean.showTeacherTime ||
        widget.cubean.teacherSignStartTime == null) {
      return SizedBox();
    }
    return Container(
      margin: EdgeInsets.only(top: 10),
      color: Colors.grey[200],
      padding: EdgeInsets.all(3),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            children: [
              Text("教师上课签到信息"),
            ],
          ),
          buildTeacherSginToTimeWidget(),
          buildTeacherSginUpTimeWidget(),
        ],
      ),
    );
  }

  buildTeacherSginToTimeWidget() {
    return Container(
      child: Wrap(
        crossAxisAlignment: WrapCrossAlignment.center,
        children: [
          Text(
            "开始上课时间： ",
            style: TextStyle(fontSize: 14, color: Color(0xff666666)),
          ),
          Text(
            "${widget.cubean.teacherSignStartTime} ",
            style: TextStyle(fontSize: 14, color: Color(0xff666666)),
          ),
        ],
      ),
    );
  }

  buildBottomButtonRow() {
    return Row(
      children: [
        buildTeacherStartButton(),
      ],
    );
  }

  buildTeacherStartButton() {
    bool showTeacherStartSign = widget.cubean.showTeacherStartSign;
    if (showTeacherStartSign) {
      return ElevatedButton(
          onPressed: () {
            showStartCourseTips();
          },
          child: _signLoading ? LoadingWidget() : Text("开始上课"));
    }
    bool showTeacherEndSign = widget.cubean.showTeacherEndSign;
    if (showTeacherEndSign) {
      return ElevatedButton(
          onPressed: () {
            showEndCourseTips();
          },
          child: _signLoading ? LoadingWidget() : Text("结束上课"));
    }
    return SizedBox();
  }

  bool _signLoading = false;
  bool _signUpLoading = false;

  ///-----------------------教师 start -----------------------------------------------------------------------
  ///开始上课
  showStartCourseTips() {
    showCommonAlertDialog(
        context: context,
        cancleText: "取消",
        selectText: "确定",
        contentMessag: "确定开始上课吗??",
        selectCallBack: () {
          teacherSignRequest();
        });
  }

  void teacherSignRequest() async {
    setState(() {
      _signLoading = true;
    });
    TrainCourseSignBean trainCourseSignBean =
        await trainTeacherCourseSign(widget.cubean);
    if (trainCourseSignBean != null) {
      ToastUtils.showToast("操作成功");
      widget.cubean.teacherSignStartTime = trainCourseSignBean.signToTime;
      widget.cubean.signRecoderId = trainCourseSignBean.id;
      widget.cubean.showTeacherStartSign = false;
      widget.cubean.showTeacherEndSign = true;
      if (Get.isRegistered<TeacherTrainDetailsController>()) {
        Get.find<TeacherTrainDetailsController>().update(["root"]);
      }
    }
    setState(() {
      _signLoading = false;
    });
  }

  showEndCourseTips() {
    showCommonAlertDialog(
        context: context,
        cancleText: "取消",
        selectText: "确定",
        contentMessag: "确定结束上课吗??",
        selectCallBack: () {
          teacherSignUpRequest();
        });
  }

  void teacherSignUpRequest() async {
    setState(() {
      _signLoading = true;
    });
    TrainCourseSignBean trainCourseSignBean =
        await trainTeacherCourseSignUp(widget.cubean);
    if (trainCourseSignBean != null) {
      ToastUtils.showToast("操作成功");
      widget.cubean.teacherSignEndTime = trainCourseSignBean.signUpTime;
      widget.cubean.showTeacherStartSign = false;
      widget.cubean.showTeacherEndSign = false;
      if (Get.isRegistered<TeacherTrainDetailsController>()) {
        Get.find<TeacherTrainDetailsController>().update(["root"]);
      }
    }
    setState(() {
      _signLoading = false;
    });
  }
}
